<?php

namespace App\Console\Commands;

use App\Api\Models\LogInStorageModel;
use App\Api\Models\PackageContentModel;
use App\Api\Models\StorageManageModel;
use App\V3\Api\Models\StorageShelfLocationModel;
use App\V3\Api\Models\StorageShelfModel;
use App\V3\Api\Models\VipOrderModel;
use App\V3\V3_1\Models\SalesReturnGoodsDetailModel;
use App\V3\V3_1\Models\StorageModel;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class storage_shelf extends Command
{
    //php artisan shelf:create

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'shelf:create';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '根据仓库创建货架和库位';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        set_time_limit(0);
        #dd(123);
        self::add_shelf_location();
    }

    //初始化货架和库位
    public static function add_shelf_location()
    {
//        $storage_list = StorageModel::select('*')->where(['delete_flag' => 0])->get();
        $storage_list = StorageModel::select('*')->get();
        DB::beginTransaction();

        try {
            foreach ($storage_list as $k => $v){
                $shelf_obj = StorageShelfModel::updateOrCreate([
                    'storage_id' => $v['id'],
                    'main_id' => $v['main_id'],
                    'status' => StorageShelfModel::USEDSTATUS,
                    'shelf_type' => StorageShelfModel::DEFAULTTYPE,
                ],[
                    'shelf_name' =>'默认货架',
                    'main_id' => $v['main_id'],
                    'storage_id' => $v['id'],
                    'status' => StorageShelfModel::USEDSTATUS,
                    'shelf_type' => StorageShelfModel::DEFAULTTYPE,

                ]);

                $shelf_id = $shelf_obj->id;
                $location_obj = StorageShelfLocationModel::updateOrCreate([
                    'storage_id' => $v['id'],
                    'main_id' => $v['main_id'],
                    'shelf_id' => $shelf_id,
                    'status' => StorageShelfModel::USEDSTATUS,
                    'location_type' => StorageShelfLocationModel::DEFAULTTYPE,

                ],[
                    'location_name' =>'默认库位',
                    'main_id' => $v['main_id'],
                    'storage_id' => $v['id'],
                    'shelf_id' => $shelf_id,
                    'status' => StorageShelfModel::USEDSTATUS,
                    'location_type' => StorageShelfLocationModel::DEFAULTTYPE,

                ]);

                DB::commit();
                echo '已完成-'.$v['id'].PHP_EOL;
            }
        } catch (\Exception $e) {
            DB::rollBack();
        }
    }

}
